package com.action;

import com.datasource.DynamicDataSources;
import com.model.UserModel;
import com.service.IShardingJdbcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("shard/test")
public class ShardingJdbcController {

    @Autowired
    private IShardingJdbcService iShardingJdbcService;

    @RequestMapping("/insert")
    @ResponseBody
    public String insert() {
        DynamicDataSources.setDataSourceKey("shardingDataSource");
        String msg = iShardingJdbcService.insert();
        return msg;
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(UserModel userModel) {
        DynamicDataSources.setDataSourceKey("shardingDataSource");
        String msg = iShardingJdbcService.update(userModel);
        return msg;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Integer orderId) {
        DynamicDataSources.setDataSourceKey("shardingDataSource");
        String msg = iShardingJdbcService.delete(orderId);
        return msg;
    }
    @RequestMapping("/list")
    @ResponseBody
    public String list() {
        String msg = iShardingJdbcService.list();
        return msg;
    }

    @RequestMapping("/listById")
    @ResponseBody
    public String listById(Integer orderId) {
        DynamicDataSources.setDataSourceKey("dataSource");
        String msg = iShardingJdbcService.listById(orderId);
        return msg;
    }

    @RequestMapping("/listByShard")
    @ResponseBody
    public String listById1(Integer orderId) {
        DynamicDataSources.setDataSourceKey("shardingDataSource");
        String msg = iShardingJdbcService.listById(orderId);
        return msg;
    }

}